import datetime
from hanchlogger import logger
import hanchxl
import time


class GongDanCountInFo(object):
    def __init__(self, name_=''):
        self.name = name_
        self.numDecimals = 2
        self.CountAll = 0
        self.CountShi = 0
        self.CountQu = 0
        self.CountHuiFangShiBai = 0
        self.CountWuXiaoHuiFang = 0
        self.CountWeiHuiFang = 0
        self.CountYouXiaoHuiFang = 0
        self.CountYouXiaoHuiFangShi = 0
        self.CountYouXiaoHuiFangQu = 0
        self.CountShuangMan = 0
        self.CountShuangManShi = 0
        self.CountShuangManQu = 0
        self.CountSheFou = 0
        self.CountSheFouShi = 0
        self.CountSheFouQu = 0
        self.lianXi = 0
        self.lianXiShi = 0
        self.lianXiQu = 0
        self.weiLanXi = 0
        self.weiLanXiShi = 0
        self.weiLanXiQu = 0
        self.jieJue = 0
        self.jieJueShi = 0
        self.jieJueQu = 0
        self.weiJieJue = 0
        self.weiJieJueShi = 0
        self.weiJieJueQu = 0
        self.manYi = 0
        self.manYiShi = 0
        self.manYiQu = 0
        self.jiBenManYi = 0
        self.jiBenManYiShi = 0
        self.jiBenManYiQu = 0
        self.buManYi = 0
        self.buManYiShi = 0
        self.buManYiQu = 0
        self.jianMian = 0
        self.jianMianShi = 0
        self.jianMianQu = 0
        self.weiJianMian = 0
        self.weiJianMianShi = 0
        self.weiJianMianQu = 0
        self.lvLianXi = 0
        self.lvLianXiShi = 0
        self.lvLianXiQu = 0
        self.lvJieJue = 0
        self.lvJieJueShi = 0
        self.lvJieJueQu = 0
        self.lvManYi = 0
        self.lvManYiShi = 0
        self.lvManYiQu = 0
        self.lvJianMian = 0
        self.lvJianMianShi = 0
        self.lvJianMianQu = 0
        self.deFen = 0
        self.shiFen = 0
        self.suoShuXiaQu = ''


class GongDanCountInFoList(GongDanCountInFo):
    def __init__(self, name_=''):
        super(GongDanCountInFoList, self).__init__(name_)
        self.listGogDanInfo = {}

    def load_gong_dan_info_by_column(self, sheet_load, sheet_quanbu, list_replace, is_do_san_lv=0, start_ping_jia=0, end_ping_jia=0):
        maxRowLoad = sheet_load.max_row
        colCode = hanchxl.get_col_index(sheet_load, '工单编号')
        sheet_load.insert_cols(colCode + 1, 3)
        sheet_load.cell(1, colCode + 1).value = '承办部门'
        sheet_load.cell(1, colCode + 2).value = '参与部门'
        sheet_load.cell(1, colCode + 3).value = '是否处理'
        colChengBan = hanchxl.get_col_index(sheet_load, '承办部门')
        colZhuangTai = hanchxl.get_col_index(sheet_load, '回访状态')
        colLeiXing = hanchxl.get_col_index(sheet_load, '派单类型')
        colLianXi = hanchxl.get_col_index(sheet_load, '是否联系')
        colJieJue = hanchxl.get_col_index(sheet_load, '是否解决')
        colManYi = hanchxl.get_col_index(sheet_load, '是否满意')
        colJianMian = hanchxl.get_col_index(sheet_load, '是否见面')
        colPaiDan = hanchxl.get_col_index(sheet_load, '派单时间')
        hanchxl.pi_pei_te_ding_col(sheet_load, '工单编号', sheet_quanbu, '承办部门', '参与部门')
        for buMen in list_replace:
            if buMen.isAll is True:
                hanchxl.xiu_gai_cheng_ban_bu_men_all(sheet_load, '承办部门', buMen.nameYuan, buMen.nameNow)
                hanchxl.xiu_gai_cheng_ban_bu_men_all(sheet_load, '参与部门', buMen.nameYuan, buMen.nameNow)
            else:
                hanchxl.xiu_gai_cheng_ban_bu_men(sheet_load, '承办部门', buMen.nameYuan, buMen.nameNow)
                hanchxl.xiu_gai_cheng_ban_bu_men(sheet_load, '参与部门', buMen.nameYuan, buMen.nameNow)
        hanchxl.remove_repeat_value(sheet_load, '承办部门', '参与部门')

        for row in range(2, maxRowLoad + 1):
            valueCode = '{}'.format(sheet_load.cell(row, colCode).value).strip()
            valueChengBan = '{}'.format(sheet_load.cell(row, colChengBan).value).strip()
            valuePaiDan = '{}'.format(sheet_load.cell(row, colPaiDan).value).strip()
            if valueCode == '':
                logger.debug('{}行  工单号为空'.format(row))
                continue
            if sheet_quanbu is not None and (valueChengBan == '' or valueChengBan == 'None'):
                logger.debug('工单[{}] 未匹配到承办部门'.format(valueCode))
                continue
            timeValuePaiDanFormat = time.strptime(valuePaiDan, '%Y-%m-%d %H:%M:%S')
            timeStampValuePaiDan = int(time.mktime(timeValuePaiDanFormat))
            logger.debug('{}行  工单编号：{}  派单时间：{}'.format(row, valueCode, valuePaiDan))
            if start_ping_jia != end_ping_jia:
                if timeStampValuePaiDan < start_ping_jia or timeStampValuePaiDan > end_ping_jia:
                    logger.debug('{}行  工单编号：{}  派单时间：{} 不在本评价期内'.format(row, valueCode, valuePaiDan))
                    continue
            # logger.debug('工单[{}]   承办部门:{}'.format(valueCode, valueChengBan))
            if valueChengBan in self.listGogDanInfo:
                infoCount = self.listGogDanInfo[valueChengBan]
            else:
                infoCount = GongDanCountInFo(valueChengBan)
            valueLeiXing = '{}'.format(sheet_load.cell(row, colLeiXing).value).strip()
            isShiPai = True
            self.CountAll = self.CountAll + 1
            infoCount.CountAll = infoCount.CountAll + 1
            logger.debug('{}行 工单[{}] 承办部门:{} 工单总数：{}'.format(row, valueCode, valueChengBan, infoCount.CountAll))
            if valueLeiXing == '直派' or valueLeiXing == '市派':
                self.CountShi = self.CountShi + 1
                infoCount.CountShi = infoCount.CountShi + 1
            elif valueLeiXing == '转派' or valueLeiXing == '区派':
                self.CountQu = self.CountQu + 1
                infoCount.CountQu = infoCount.CountQu + 1
                isShiPai = False

            if is_do_san_lv > 0:
                valueZhuanbgTai = '{}'.format(sheet_load.cell(row, colZhuangTai).value).strip()
                # logger.debug('工单[{}] 回访状态：{}'.format(valueCode, valueZhuanbgTai))
                if valueZhuanbgTai != '有效回访':
                    if valueZhuanbgTai == '未回访':
                        self.CountWeiHuiFang = self.CountWeiHuiFang + 1
                        infoCount.CountWeiHuiFang = infoCount.CountWeiHuiFang + 1
                    if valueZhuanbgTai == '无效回访':
                        self.CountWuXiaoHuiFang = self.CountWuXiaoHuiFang + 1
                        infoCount.CountWuXiaoHuiFang = infoCount.CountWuXiaoHuiFang + 1
                    if valueZhuanbgTai == '无效回访':
                        self.CountWuXiaoHuiFang = self.CountWuXiaoHuiFang + 1
                        infoCount.CountWuXiaoHuiFang = infoCount.CountWuXiaoHuiFang + 1
                    self.listGogDanInfo[valueChengBan] = infoCount
                    continue

                self.CountYouXiaoHuiFang = self.CountYouXiaoHuiFang + 1
                infoCount.CountYouXiaoHuiFang = infoCount.CountYouXiaoHuiFang + 1
                if isShiPai is True:
                    self.CountYouXiaoHuiFangShi = self.CountYouXiaoHuiFangShi + 1
                    infoCount.CountYouXiaoHuiFangShi = infoCount.CountYouXiaoHuiFangShi + 1
                else:
                    self.CountYouXiaoHuiFangQu = self.CountYouXiaoHuiFangQu + 1
                    infoCount.CountYouXiaoHuiFangQu = infoCount.CountYouXiaoHuiFangQu + 1
                valueLianXi = '{}'.format(sheet_load.cell(row, colLianXi).value).strip()
                valueJieJue = '{}'.format(sheet_load.cell(row, colJieJue).value).strip()
                valueManYi = '{}'.format(sheet_load.cell(row, colManYi).value).strip()
                valueJianMian = '{}'.format(sheet_load.cell(row, colJianMian).value).strip()
                isShuangMan = 0
                isSheFou = 0
                if valueLianXi == '已联系' or valueLianXi == '联系':
                    self.lianXi = self.lianXi + 1
                    infoCount.lianXi = infoCount.lianXi + 1
                    if isShiPai is True:
                        self.lianXiShi = self.lianXiShi + 1
                        infoCount.lianXiShi = infoCount.lianXiShi + 1
                    else:
                        self.lianXiQu = self.lianXiQu + 1
                        infoCount.lianXiQu = infoCount.lianXiQu + 1
                if valueLianXi == '未联系':
                    self.weiLanXi = self.weiLanXi + 1
                    infoCount.weiLanXi = infoCount.weiLanXi + 1
                    if isShiPai is True:
                        self.weiLanXiShi = self.weiLanXiShi + 1
                        infoCount.weiLanXiShi = infoCount.weiLanXiShi + 1
                    else:
                        self.weiLanXiQu = self.weiLanXiQu + 1
                        infoCount.weiLanXiQu = infoCount.weiLanXiQu + 1
                if valueJieJue == '已解决' or valueJieJue == '解决':
                    self.jieJue = self.jieJue + 1
                    infoCount.jieJue = infoCount.jieJue + 1
                    isShuangMan = isShuangMan + 1
                    if isShiPai is True:
                        self.jieJueShi = self.jieJueShi + 1
                        infoCount.jieJueShi = infoCount.jieJueShi + 1
                    else:
                        self.jieJueQu = self.jieJueQu + 1
                        infoCount.jieJueQu = infoCount.jieJueQu + 1
                if valueJieJue == '未解决':
                    self.weiJieJue = self.weiJieJue + 1
                    infoCount.weiJieJue = infoCount.weiJieJue + 1
                    isSheFou = isSheFou + 1
                    if isShiPai is True:
                        self.weiJieJueShi = self.weiJieJueShi + 1
                        infoCount.weiJieJueShi = infoCount.weiJieJueShi + 1
                    else:
                        self.weiJieJueQu = self.weiJieJueQu + 1
                        infoCount.weiJieJueQu = infoCount.weiJieJueQu + 1
                if valueManYi == '满意' or valueManYi == '非常满意':
                    self.manYi = self.manYi + 1
                    infoCount.manYi = infoCount.manYi + 1
                    isShuangMan = isShuangMan + 1
                    if isShiPai is True:
                        self.manYiShi = self.manYiShi + 1
                        infoCount.manYiShi = infoCount.manYiShi + 1
                    else:
                        self.manYiQu = self.manYiQu + 1
                        infoCount.manYiQu = infoCount.manYiQu + 1
                if valueManYi == '基本满意':
                    self.jiBenManYi = self.jiBenManYi + 1
                    infoCount.jiBenManYi = infoCount.jiBenManYi + 1
                    if isShiPai is True:
                        self.jiBenManYiShi = self.jiBenManYiShi + 1
                        infoCount.jiBenManYiShi = infoCount.jiBenManYiShi + 1
                    else:
                        self.jiBenManYiQu = self.jiBenManYiQu + 1
                        infoCount.jiBenManYiQu = infoCount.jiBenManYiQu + 1
                if valueManYi == '不满意' or valueManYi == '非常不满意':
                    self.buManYi = self.buManYi + 1
                    infoCount.buManYi = infoCount.buManYi + 1
                    isSheFou = isSheFou + 1
                    if isShiPai is True:
                        self.buManYiShi = self.buManYiShi + 1
                        infoCount.buManYiShi = infoCount.buManYiShi + 1
                    else:
                        self.buManYiQu = self.buManYiQu + 1
                        infoCount.buManYiQu = infoCount.buManYiQu + 1
                if valueJianMian == '见面' or valueJianMian == '已见面':
                    self.jianMian = self.jianMian + 1
                    infoCount.jianMian = infoCount.jianMian + 1
                    if isShiPai is True:
                        self.jianMianShi = self.jianMianShi + 1
                        infoCount.jianMianShi = infoCount.jianMianShi + 1
                    else:
                        self.jianMianQu = self.jianMianQu + 1
                        infoCount.jianMianQu = infoCount.jianMianQu + 1
                if valueJianMian == '未见面':
                    self.weiJianMian = self.weiJianMian + 1
                    infoCount.weiJianMian = infoCount.weiJianMian + 1
                    if isShiPai is True:
                        self.weiJianMianShi = self.weiJianMianShi + 1
                        infoCount.weiJianMianShi = infoCount.weiJianMianShi + 1
                    else:
                        self.weiJianMianQu = self.weiJianMianQu + 1
                        infoCount.weiJianMianQu = infoCount.weiJianMianQu + 1
                if isShuangMan == 2:
                    self.CountShuangMan = self.CountShuangMan + 1
                    infoCount.CountShuangMan = infoCount.CountShuangMan + 1
                    if isShiPai is True:
                        self.CountShuangManShi = self.CountShuangManShi + 1
                        infoCount.CountShuangManShi = infoCount.CountShuangManShi + 1
                    else:
                        self.CountShuangManQu = self.CountShuangManQu + 1
                        infoCount.CountShuangManQu = infoCount.CountShuangManQu + 1
                if isSheFou == 2:
                    self.CountSheFou = self.CountSheFou + 1
                    infoCount.CountSheFou = infoCount.CountSheFou + 1
                    if isShiPai is True:
                        self.CountSheFouShi = self.CountSheFouShi + 1
                        infoCount.CountSheFouShi = infoCount.CountSheFouShi + 1
                    else:
                        self.CountSheFouQu = self.CountSheFouQu + 1
                        infoCount.CountSheFouQu = infoCount.CountSheFouQu + 1
            self.listGogDanInfo[valueChengBan] = infoCount
        # for buMen in self.listGogDanInfo:
        #     logger.debug('承办部门[{}] ：双满件数：{}  涉否件数：{}'.format(buMen, self.listGogDanInfo[buMen].CountShuangMan, self.listGogDanInfo[buMen].CountSheFou))
        for item in self.listGogDanInfo:
            infoCount = self.listGogDanInfo[item]
            logger.debug('承办部门[{}] ：有效回访 {} 见面（区）：{} 见面（市）：{}'.format(item, infoCount.CountYouXiaoHuiFang, infoCount.jianMianQu, infoCount.jianMianShi))
            if is_do_san_lv == 1:
                if infoCount.CountYouXiaoHuiFangShi > 0:
                    infoCount.lvLianXiShi = round(infoCount.lianXiShi * 100 / infoCount.CountYouXiaoHuiFangShi, infoCount.numDecimals)
                    infoCount.lvJieJueShi = round(infoCount.jieJueShi * 100 / infoCount.CountYouXiaoHuiFangShi, infoCount.numDecimals)
                    infoCount.lvManYiShi = round((infoCount.manYiShi + infoCount.jiBenManYiShi*0.8) * 100 / infoCount.CountYouXiaoHuiFangShi,
                                            infoCount.numDecimals)
                    # infoCount.lvJianMianShi = round(infoCount.jianMianShi * 100 / infoCount.CountYouXiaoHuiFangShi, infoCount.numDecimals)
                if infoCount.CountYouXiaoHuiFangQu > 0:
                    infoCount.lvLianXiQu = round(infoCount.lianXiQu * 100 / infoCount.CountYouXiaoHuiFangQu, infoCount.numDecimals)
                    infoCount.lvJieJueQu = round(infoCount.jieJueQu * 100 / infoCount.CountYouXiaoHuiFangQu, infoCount.numDecimals)
                    infoCount.lvManYiQu = round((infoCount.manYiQu + infoCount.jiBenManYiQu*0.8) * 100 / infoCount.CountYouXiaoHuiFangQu,
                                           infoCount.numDecimals)
                    # infoCount.lvJianMianQu = round(infoCount.jianMianQu * 100 / infoCount.CountYouXiaoHuiFangQu, infoCount.numDecimals)
                if infoCount.CountYouXiaoHuiFangQu > 0 and infoCount.CountYouXiaoHuiFangShi > 0:
                    infoCount.lvLianXi = round((infoCount.lvLianXiQu + infoCount.lvLianXiShi) / 2, infoCount.numDecimals)
                    infoCount.lvJieJue = round((infoCount.lvJieJueQu + infoCount.lvJieJueShi) / 2, infoCount.numDecimals)
                    infoCount.lvManYi = round((infoCount.lvManYiQu + infoCount.lvManYiShi) / 2, infoCount.numDecimals)
                    # infoCount.lvJianMian = round((infoCount.lvJianMianQu + infoCount.lvJianMianShi) / 2, infoCount.numDecimals)
                    infoCount.lvJianMian = round(((infoCount.jianMianShi + infoCount.jianMianQu) * 100) / infoCount.CountYouXiaoHuiFang, infoCount.numDecimals)
                elif infoCount.CountYouXiaoHuiFangQu > 0:
                    infoCount.lvLianXi = infoCount.lvLianXiQu
                    infoCount.lvJieJue = infoCount.lvJieJueQu
                    infoCount.lvManYi = infoCount.lvManYiQu
                    infoCount.lvJianMian = round(infoCount.jianMianQu * 100 / infoCount.CountYouXiaoHuiFangQu, infoCount.numDecimals)
                elif infoCount.CountYouXiaoHuiFangShi > 0:
                    infoCount.lvLianXi = infoCount.lvLianXiShi
                    infoCount.lvJieJue = infoCount.lvJieJueShi
                    infoCount.lvManYi = infoCount.lvManYiShi
                    infoCount.lvJianMian = round(infoCount.jianMianShi * 100 / infoCount.CountYouXiaoHuiFangShi, infoCount.numDecimals)
                infoCount.deFen = round(infoCount.lvLianXi*0.1+infoCount.lvJieJue*0.5+infoCount.lvManYi*0.4, infoCount.numDecimals)
            elif is_do_san_lv == 2:
                if infoCount.CountYouXiaoHuiFang > 0:
                    infoCount.lvLianXi = round((infoCount.lianXiShi + infoCount.lianXiQu) * 100 / infoCount.CountYouXiaoHuiFang, infoCount.numDecimals)
                    infoCount.lvJieJue = round((infoCount.jieJueShi + infoCount.jieJueQu) * 100 / infoCount.CountYouXiaoHuiFang, infoCount.numDecimals)
                    infoCount.lvManYi = round((infoCount.manYiShi + infoCount.jiBenManYiShi * 0.9 + infoCount.manYiQu + infoCount.jiBenManYiQu * 0.9) * 100 / infoCount.CountYouXiaoHuiFang,
                                         infoCount.numDecimals)
                    infoCount.lvJianMian = round((infoCount.jianMianShi + infoCount.jianMianQu) * 100 / infoCount.CountYouXiaoHuiFang, infoCount.numDecimals)
            self.listGogDanInfo[item] = infoCount
        if is_do_san_lv == 1:
            if self.CountYouXiaoHuiFangShi > 0:
                self.lvLianXiShi = round(self.lianXiShi * 100 / self.CountYouXiaoHuiFangShi, self.numDecimals)
                self.lvJieJueShi = round(self.jieJueShi * 100 / self.CountYouXiaoHuiFangShi, self.numDecimals)
                self.lvManYiShi = round((self.manYiShi + self.jiBenManYiShi * 0.9) * 100 / self.CountYouXiaoHuiFangShi,
                                        self.numDecimals)
                # self.lvJianMianShi = round(self.jianMianShi * 100 / self.CountYouXiaoHuiFangShi, self.numDecimals)
            if self.CountYouXiaoHuiFangQu > 0:
                self.lvLianXiQu = round(self.lianXiQu * 100 / self.CountYouXiaoHuiFangQu, self.numDecimals)
                self.lvJieJueQu = round(self.jieJueQu * 100 / self.CountYouXiaoHuiFangQu, self.numDecimals)
                self.lvManYiQu = round((self.manYiQu + self.jiBenManYiQu * 0.9) * 100 / self.CountYouXiaoHuiFangQu,
                                       self.numDecimals)
                # self.lvJianMianQu = round(self.jianMianQu * 100 / self.CountYouXiaoHuiFangQu, self.numDecimals)
            if self.CountYouXiaoHuiFangQu > 0 and self.CountYouXiaoHuiFangShi > 0:
                self.lvLianXi = round((self.lvLianXiQu + self.lvLianXiShi) / 2, self.numDecimals)
                self.lvJieJue = round((self.lvJieJueQu + self.lvJieJueShi) / 2, self.numDecimals)
                self.lvManYi = round((self.lvManYiQu + self.lvManYiShi) / 2, self.numDecimals)
                # self.lvJianMian = round((self.lvJianMianQu + self.lvJianMianShi) / 2, self.numDecimals)
                self.lvJianMian = round(((self.jianMianShi + self.jianMianQu) * 100) / self.CountYouXiaoHuiFang, self.numDecimals)
            elif self.CountYouXiaoHuiFangQu > 0:
                self.lvLianXi = self.lvLianXiQu
                self.lvJieJue = self.lvJieJueQu
                self.lvManYi = self.lvManYiQu
                self.lvJianMian = round(self.jianMianQu * 100 / self.CountYouXiaoHuiFangQu, self.numDecimals)
            else:
                self.lvLianXi = self.lvLianXiShi
                self.lvJieJue = self.lvJieJueShi
                self.lvManYi = self.lvManYiShi
                self.lvJianMian = round(self.jianMianShi * 100 / self.CountYouXiaoHuiFangShi, self.numDecimals)
            self.deFen = round(self.lvLianXi * 0.1 + self.lvJieJue * 0.5 + self.lvManYi * 0.4,
                                    self.numDecimals)
        elif is_do_san_lv == 2:
            if self.CountYouXiaoHuiFang > 0:
                self.lvLianXi = round((self.lianXiShi + self.lianXiQu) * 100 / self.CountYouXiaoHuiFang,
                                      self.numDecimals)
                self.lvJieJue = round((self.jieJueShi + self.jieJueQu) * 100 / self.CountYouXiaoHuiFang,
                                      self.numDecimals)
                self.lvManYi = round((
                                                 self.manYiShi + self.jiBenManYiShi * 0.9 + self.manYiQu + self.jiBenManYiQu * 0.9) * 100 / self.CountYouXiaoHuiFang,
                                     self.numDecimals)
                self.lvJianMian = round((self.jianMianShi + self.jianMianQu) * 100 / self.CountYouXiaoHuiFang,
                                        self.numDecimals)
        if is_do_san_lv == 1:
            self.listGogDanInfo = dict(sorted(self.listGogDanInfo.items(), key=lambda kv: kv[1].deFen, reverse=True))
        elif is_do_san_lv == 2:
            self.listGogDanInfo = dict(sorted(self.listGogDanInfo.items(), key=lambda kv: kv[1].CountYouXiaoHuiFang, reverse=True))
        else:
            self.listGogDanInfo = dict(sorted(self.listGogDanInfo.items(), key=lambda kv: kv[1].CountAll, reverse=True))